package com.shop.controller;

import com.github.pagehelper.PageInfo;
import com.shop.base.BaseController;
import com.shop.entity.Address;
import com.shop.result.ResultVO;
import com.shop.service.AddressService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/address")
public class AddressController extends BaseController {
    private final AddressService addressService;

    /**
     * 获取地址列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/listAddress")
    public PageInfo<Address> listAddress(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        return addressService.listAddress(pageNum, pageSize);
    }

    @GetMapping("/listWithAddressMappers")
    public List<Address> listWithAddressMappers(@RequestParam("id") Integer id){
        return addressService.listWithAddressMappers(id);
    }

    /**
     * 根据id获取地址
     * @param id
     * @return
     */
    @GetMapping("/getAddressById")
    public Address getAddressById(@RequestParam("id") Integer id){
        return addressService.getAddressById(id);
    }

    /**
     * 添加地址
     * @param address
     */
    @PostMapping("/addAddress")
    public ResultVO<Address> addAddress(@RequestBody Address address){
        addressService.addAddress(address);
        return success(200,"添加成功");
    }

    /**
     * 删除地址
     * @param id
     */
    @GetMapping ("/deleteAddress")
    public ResultVO<Object> deleteAddress(@RequestParam("id") Integer id){
        addressService.deleteAddress(id);
        return success(200,"删除成功");
    }

    /**
     * 修改地址
     * @param address
     */
    @GetMapping ("/updateAddress")
    public void updateAddress(@RequestParam("address") Address address){
        addressService.updateAddress(address);
    }
    @GetMapping("/getTotal")
    public int getTotal(){
        return addressService.getTotal();
    }

}
